package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		/*Check empty field*/
		if ( request.getParameter("username").equals("") || request.getParameter("password").equals("") ){
			request.setAttribute("erreur","You have to fill all fields");
			request.getRequestDispatcher( "index.jsp" ).forward(request,response);
			return;
		}
		
		/*Check if username equals password*/
		if ( request.getParameter("username").equals(request.getParameter("password"))){	
						
			/*
			IDAOContact dao = new DAOContact();
			dao.addContact("kevin", "pozderec", "kpozderec@gmail.com", "30 rue de la croix buard", "garges-les-gonesse", "95140", "FRANCE");
			dao.addContact("kevin", "bacon", "kbacon@gmail.com", "68 jump street", "Los Angeles", "88789", "USA");
			dao.addContact("jean", "dupont", "jdupont@gmail.com", "7 rue de la fontaine", "paris", "75390", "FRANCE");
			*/
			
			request.getRequestDispatcher( "Menu.jsp" ).forward(request,response);	
			
		} else {
			
			request.setAttribute("erreur","BETA VERSION : Username has to be equals to password");
			request.getRequestDispatcher( "index.jsp" ).forward(request,response);
			
		}
		
	}

}
